package com.ywjt.platformbusiness.my.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseVPFragment;
import com.ywjt.platformbusiness.constants.UrlConstants;
import com.ywjt.platformbusiness.http.HttpRequest;
import com.ywjt.platformbusiness.http.HttpStringCallBack;
import com.ywjt.platformbusiness.my.activity.ActivityAddAddress;
import com.ywjt.platformbusiness.my.adapter.AddressAdapter;
import com.ywjt.platformbusiness.my.adapter.CouponAdapter;
import com.ywjt.platformbusiness.my.model.AddressModel;
import com.ywjt.platformbusiness.my.model.MyCouponListModel;
import com.ywjt.platformbusiness.util.JsonUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CouponFragment extends BaseVPFragment {
    private CouponAdapter adapter;
    private RecyclerView rcView;
    private View view;
    private Context context;
    private List<String> data;
    private LinearLayout llNodata;

    private int currentPage=1;
    private SmartRefreshLayout refresh;
    private int count=0;
    private int status=0;

    public static CouponFragment newInstance(int status ) {
        Bundle arguments = new Bundle();
        arguments.putInt("status",status);
       CouponFragment fl = new CouponFragment();
        fl.setArguments(arguments);
        return fl;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_order, container, false);
            context = getActivity();
            initView();
        }
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
    }

    @Override
    protected void onFragmentFirstVisible() {
        getData();
    }

    public void initView(){
        status=getArguments().getInt("status",0);
        refresh=view.findViewById(R.id.refresh);
        rcView=view.findViewById(R.id.rcView);
        llNodata=view.findViewById(R.id.llNodata);
    }

    //获取地址列表
    public void getData(){
        new HttpRequest(getContext()).doGet(UrlConstants.myCouponsList_isExpir+status, "",new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                //把请求得数据转换成json对象
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success=jsonObject.getBoolean("success");
                    if(success){
                        Object obj = JsonUtils.fromJson(result.toString(), MyCouponListModel.class);
                        if (obj instanceof MyCouponListModel) {
                            MyCouponListModel myCouponListModel =(MyCouponListModel)obj;
                            if (myCouponListModel.getResult().size() > 0) {
                                llNodata.setVisibility(View.GONE);
                                rcView.setVisibility(View.VISIBLE);
                                /*****/
                               // adapter=new CouponAdapter(myCouponListModel.getResult(),status);
                                rcView.setLayoutManager(new LinearLayoutManager(getContext()));
                               // rcView.setAdapter(adapter);
                                /****/
                                if (adapter.getItemCount() < count) {
                                    //还有多的数据
                                    refresh.finishRefresh();
                                } else {
                                    //没有更多数据（上拉加载功能将显示没有更多数据）
                                    refresh.finishRefreshWithNoMoreData();
                                }
                            } else {
                                rcView.setVisibility(View.GONE);
                                refresh.finishRefreshWithNoMoreData();
                                llNodata.setVisibility(View.VISIBLE);
                            }

                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                refresh.finishLoadMore();
                refresh.finishRefresh();
            }
        });
    }
}